package com.hd123.rumba.gwt.widget2.client.hotkey;

import com.google.gwt.dom.client.NativeEvent;

/**
 * 热键定义
 * 
 * @author LiQi
 * 
 */
public class RHotKey {

  /** 是否按下Ctrl键 */
  private boolean ctrlKey = false;

  /** 是否按下Alt键 */
  private boolean altKey = false;

  /** 是否按下Shift键 */
  private boolean shiftKey = false;

  /** 键值 */
  private int keyCode = 0;

  /** 是否启用 */
  private boolean enabled = true;

  public RHotKey() {
  }

  /**
   * @param ctrlKey
   *          是否按下Ctrl键
   * @param altKey
   *          是否按下Alt键
   * @param shiftKey
   *          是否按下Shift键
   * @param keyCode
   *          键值
   */
  public RHotKey(boolean ctrlKey, boolean altKey, boolean shiftKey, int keyCode) {
    this();
    setCtrlKey(ctrlKey);
    setAltKey(altKey);
    setShiftKey(shiftKey);
    setKeyCode(keyCode);
  }

  /**
   * 是否按下Ctrl键
   * 
   * @return
   */
  public boolean isCtrlKey() {
    return ctrlKey;
  }

  /**
   * 设置是否按下Ctrl键
   * 
   * @param ctrlKey
   */
  public void setCtrlKey(boolean ctrlKey) {
    this.ctrlKey = ctrlKey;
  }

  /**
   * 是否按下Alt键
   * 
   * @return
   */
  public boolean isAltKey() {
    return altKey;
  }

  /**
   * 设置是否按下Alt键
   * 
   * @param altKey
   */
  public void setAltKey(boolean altKey) {
    this.altKey = altKey;
  }

  /**
   * 是否按下Shift键
   * 
   * @return
   */
  public boolean isShiftKey() {
    return shiftKey;
  }

  /**
   * 设置是否按下Shift键
   * 
   * @param shiftKey
   */
  public void setShiftKey(boolean shiftKey) {
    this.shiftKey = shiftKey;
  }

  /**
   * 键值
   * 
   * @return
   */
  public int getKeyCode() {
    return keyCode;
  }

  /**
   * 设置键值
   * 
   * @param keyCode
   */
  public void setKeyCode(int keyCode) {
    this.keyCode = keyCode;
  }

  /**
   * 是否启用
   * 
   * @return
   */
  public boolean isEnabled() {
    return enabled;
  }

  /**
   * 设置是否启用
   * 
   * @param enabled
   */
  public void setEnabled(boolean enabled) {
    this.enabled = enabled;
  }

  public boolean equals(Object obj) {
    if (!(obj instanceof RHotKey))
      return false;

    RHotKey hotKey = (RHotKey) obj;
    return ctrlKey == hotKey.isCtrlKey() && altKey == hotKey.isAltKey()
        && shiftKey == hotKey.isShiftKey() && keyCode == hotKey.getKeyCode();
  }

  public String toString() {
    StringBuffer s = new StringBuffer();
    if (ctrlKey) {
      if (s.length() > 0)
        s.append("+");
      s.append("Ctrl");
    }
    if (altKey) {
      if (s.length() > 0)
        s.append("+");
      s.append("Alt");
    }
    if (shiftKey) {
      if (s.length() > 0)
        s.append("+");
      s.append("Shift");
    }

    if (s.length() > 0)
      s.append("+");
    String keyName = RKey.getKeyName(keyCode);
    if (keyName == null)
      keyName = String.valueOf(keyCode);
    s.append(keyName);

    return s.toString();
  }

  /**
   * 是否和指定的事件匹配
   * 
   * @param event
   * @return
   */
  public boolean isMatching(NativeEvent event) {
    return ctrlKey == event.getCtrlKey() && altKey == event.getAltKey()
        && shiftKey == event.getShiftKey() && keyCode == event.getKeyCode();
  }
}
